// ==UserScript==
// @name         [kesai]磁力猫自动跳转
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       kesai
// @match        https://www.cilimao.me/search?word=*
// @match        https://www.cilimao.me/baike/movie/*
// @match        https://www.cilimao.cc/search?word=*
// @match        https://www.cilimao.cc/baike/movie/*
// @match        https://www.cilimao.space/search?word=*
// @match        https://www.cilimao.space/baike/movie/*
// @grant        none
// @require      https://code.jquery.com/jquery-3.3.1.min.js
// ==/UserScript==
(function() {
    'use strict';

    // Your code here...
    //console.log($("a:contains('下载资源')"));
    if (window.location.href.indexOf("search?word=") > 0) { //打开的是搜索页
        window.location.href = $("a:contains('下载资源')").attr("href");
    } else {
        //加载下载资源页后自动定位到下载列表
        var span = $("span:contains('智能匹配')");
        if (span) {
            span.click();
            var t = span.offset().top;
            $(window).scrollTop(t);
        }
    }



    $("ul.MovieResources__self_resouces_icon___17W30").each(function() {
        if($(this).children().children().text()!="磁链")return;
        var magnet_link = $(this).children().children().attr('href');

        /*var hashcode=magnet_link.substring(magnet_link.lastIndexOf(":")+1,magnet_link.length);
        var encode_magnet_link="magnet=magnet%253A%253Fxt%253Durn%253Abtih%253A"+hashcode.toLowerCase();
        console.log(encode_magnet_link); //encodeURIComponent(encodeURIComponent(magnet_link));
        var bt_link ='http://www.youranshare.com/torrentmagnet/magnetTotorrent.php?'+encode_magnet_link+'&timestamp=1542360337&token=2b7b1b378afadcb34d32adeca97412fa';
        var proxy_url="https://cors-anywhere.herokuapp.com";*/
        var li = $('<li class="MovieResources__ci_icon___1J-At" data-reactid="600" style="background-color:green;"><i class="iconfont icon-xiazai" ></i>种子</li>');
        /*li.click(function() {
            $.ajax({
                url:proxy_url+"/"+bt_link,
                type: "GET",
                dataType: "json",
                success: function(response) {
                    console.log(response.url);
                    if(response.result===1){
                        if(response.url){window.open(response.url);return;}
                        alert('获取bt文件错误，请联系技术');
                    }
                }
            });

        });*/
        li.click(function(){
            //window.open("http://www.torrent.org.cn/?url="+magnet_link);
            window.open("http://www.torrent.org.cn/home/convert/magnet2torrent.html?hash="+magnet_link);
        });
        $(this).append(li);
        li.css("cursor","pointer");
    });

})();